/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.report.mapper.statistics;

import com.koron.report.bean.StatisticsBean;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * 特种设备检验完成率
 */
public interface InspecCompRateSpecialEquipMapper {

    /**
     * 特种设备检验 检验完成数：按公司
     * @param startDate
     * @param stopDate
     * @param deviceTypeId 设备类型id,引用数据字典
     * @return
     */
    @Select("<script>" +
            "select  td.corp_code AS code," +
            "        count(*) AS value" +
            "        from device_calibration_relation AS dcr INNER JOIN tbl_device AS td ON dcr.device_id=td.id" +
            "        where dcr.normal is not null" +
            "        AND td.last_calibration_date between #{startDate} and #{stopDate} " +
            "        <if test='deviceTypeId != null'> AND td.device_type_id = #{deviceTypeId}</if>" +
            "        group by td.corp_code" +
            "</script>")
    List<StatisticsBean> statisticsCompany(@Param("startDate") Date startDate, @Param("stopDate") Date stopDate,
                                                        @Param("deviceTypeId") String deviceTypeId);

    /**
     * 特种设备检验 应检验总数：按公司
     *
     * @param startDate
     * @param stopDate
     * @param deviceTypeId 设备类型id,引用数据字典
     * @return
     */
    @Select("<script>" +
            "select  td.corp_code AS code," +
            "        count(*) AS value" +
            "        from device_calibration_relation AS dcr INNER JOIN tbl_device AS td ON dcr.device_id=td.id" +
            "        where td.last_calibration_date between #{startDate} and #{stopDate} " +
            "        <if test='deviceTypeId != null'> AND td.device_type_id = #{deviceTypeId}</if>" +
            "        group by td.corp_code" +
            "</script>")
    List<StatisticsBean> statisticsTotalCompany(@Param("startDate") Date startDate, @Param("stopDate") Date stopDate,
                                               @Param("deviceTypeId") String deviceTypeId);

    /**
     * 特种设备检验 检验完成数：按部门
     * @param startDate
     * @param stopDate
     * @param deviceTypeId 设备类型id,引用数据字典
     * @return
     */
    @Select("<script>" +
            "select  td.dept_code AS code, td.dept_name AS codeName," +
            "        count(*) AS value" +
            "        from device_calibration_relation AS dcr INNER JOIN tbl_device AS td ON dcr.device_id=td.id" +
            "        where dcr.normal is not null" +
            "        AND td.last_calibration_date between #{startDate} and #{stopDate} " +
            "        <if test='deviceTypeId != null'> AND td.device_type_id = #{deviceTypeId}</if>" +
            "        group by td.dept_code, td.dept_name" +
            "</script>")
    List<StatisticsBean> statisticsDept(@Param("startDate") Date startDate, @Param("stopDate") Date stopDate,
                                           @Param("deviceTypeId") String deviceTypeId);

    /**
     * 特种设备检验 应检验总数：按部门
     *
     * @param startDate
     * @param stopDate
     * @param deviceTypeId 设备类型id,引用数据字典
     * @return
     */
    @Select("<script>" +
            "select  td.dept_code AS code, td.dept_name AS codeName," +
            "        count(*) AS value" +
            "        from device_calibration_relation AS dcr INNER JOIN tbl_device AS td ON dcr.device_id=td.id" +
            "        where td.last_calibration_date between #{startDate} and #{stopDate} " +
            "        <if test='deviceTypeId != null'> AND td.device_type_id = #{deviceTypeId}</if>" +
            "        group by td.dept_code, td.dept_name" +
            "</script>")
    List<StatisticsBean> statisticsTotalDept(@Param("startDate") Date startDate, @Param("stopDate") Date stopDate,
                                                @Param("deviceTypeId") String deviceTypeId);

    /**
     * 特种设备检验 检验完成数：按室
     * @param startDate
     * @param stopDate
     * @param deviceTypeId 设备类型id,引用数据字典
     * @return
     */
    @Select("<script>" +
            "select  td.grid_code AS code, td.grid_name AS codeName," +
            "        count(*) AS value" +
            "        from device_calibration_relation AS dcr INNER JOIN tbl_device AS td ON dcr.device_id=td.id" +
            "        where dcr.normal is not null" +
            "        AND td.last_calibration_date between #{startDate} and #{stopDate} " +
            "        <if test='deviceTypeId != null'> AND td.device_type_id = #{deviceTypeId}</if>" +
            "        group by td.grid_code, td.grid_name" +
            "</script>")
    List<StatisticsBean> statisticsRoom(@Param("startDate") Date startDate, @Param("stopDate") Date stopDate,
                                           @Param("deviceTypeId") String deviceTypeId);

    /**
     * 特种设备检验 应检验总数：按室
     *
     * @param startDate
     * @param stopDate
     * @param deviceTypeId 设备类型id,引用数据字典
     * @return
     */
    @Select("<script>" +
            "select  td.grid_code AS code, td.grid_name AS codeName," +
            "        count(*) AS value" +
            "        from device_calibration_relation AS dcr INNER JOIN tbl_device AS td ON dcr.device_id=td.id" +
            "        where td.last_calibration_date between #{startDate} and #{stopDate} " +
            "        <if test='deviceTypeId != null'> AND td.device_type_id = #{deviceTypeId}</if>" +
            "        group by td.grid_code, td.grid_name" +
            "</script>")
    List<StatisticsBean> statisticsTotalRoom(@Param("startDate") Date startDate, @Param("stopDate") Date stopDate,
                                                @Param("deviceTypeId") String deviceTypeId);
}
